package com.by.service.impl;

import com.by.bean.PageBean;
import com.by.bean.ProductCategory;
import com.by.mapper.ProductCategoryMapper;
import com.by.query.ProductCategoryQuery;
import com.by.service.ProductCategoryService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * date 2025/2/25 14:21
 * author bjj
 * decoration
 * version 1.0
 * since jdk11
 */
@Service
@SuppressWarnings("all")
public class ProductCategoryServiceImpl implements ProductCategoryService {
    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Override
    public PageBean queryAll(ProductCategoryQuery productCategoryQuery) {
        PageHelper.startPage(productCategoryQuery.getPage(), productCategoryQuery.getPageSize());
        List list = productCategoryMapper.queryAll(productCategoryQuery);
        PageInfo info = new PageInfo(list);
        return new PageBean(productCategoryQuery.getPage(), productCategoryQuery.getPageSize(), info.getTotal(), list);
    }

    @Override
    public int save(ProductCategory productCategory) {
        productCategory.setCode(getNextCode(productCategory.getPid()));
        return productCategoryMapper.save(productCategory);
    }

    @Override
    public int update(ProductCategory productCategory) {
        return productCategoryMapper.update(productCategory);
    }

    @Override
    public int delete(int code) {
        return productCategoryMapper.delete(code);
    }

    int getNextCode(int pid) {
        int code=0;
        code = productCategoryMapper.queryMaxCode(pid);
        if (code ==0){
            return Integer.parseInt(pid+""+1);
        }else {
            return code+1;
        }
    }



    @Override
    public int queryMaxCode(int pid) {
        return productCategoryMapper.queryMaxCode(pid);
    }
}
